package com.yoyo.yyadsdk.util;

import com.yoyo.yyadsdk.bean.UserActive;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by danny.jiang on 17/6/20.
 */

public class StringToUserActive implements PropertyConverter<List<UserActive>, String> {
    @Override
    public List<UserActive> convertToEntityProperty(String databaseValue) {
        if (databaseValue == null || databaseValue.equals(""))
            return null;
        List<String> list = Arrays.asList(databaseValue.split(","));
        if (list.size() > 0) {
            List<UserActive> listTemp = new ArrayList<>();
            for (String s : list) {
                try {
                    String[] split = s.split(":");
                    listTemp.add(new UserActive(split[0], Integer.parseInt(split[1])));
                } catch (Exception ignored) {

                }
            }
            return listTemp;
        }
        return null;
    }

    @Override
    public String convertToDatabaseValue(List<UserActive> entityProperty) {
        if (entityProperty == null) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            for (UserActive link : entityProperty) {
                sb.append(link.getPkgName());
                sb.append(":");
                sb.append(link.getTime());
                sb.append(",");
            }
            return sb.toString();
        }
    }
}